package com.ruoyi.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.inventory.domain.ProgressDetail;
import com.ruoyi.inventory.mapper.ProgressMapper;
import com.ruoyi.inventory.domain.Progress;
import com.ruoyi.inventory.service.IProgressService;

/**
 * 工序Service业务层处理
 *
 * @author xwj
 * @date 2024-06-17
 */
@Service
public class ProgressServiceImpl extends ServiceImpl<ProgressMapper, Progress> implements IProgressService {
    @Autowired
    private ProgressMapper progressMapper;

    /**
     * 查询工序
     *
     * @param id 工序主键
     * @return 工序
     */
    @Override
    public Progress selectProgressById(Long id) {
        return progressMapper.selectProgressById(id);
    }

    /**
     * 查询工序列表
     *
     * @param progress 工序
     * @return 工序
     */
    @Override
    public List<Progress> selectProgressList(Progress progress) {
        return progressMapper.selectProgressList(progress);
    }

    /**
     * 新增工序
     *
     * @param progress 工序
     * @return 结果
     */
    @Transactional
    @Override
    public Progress insertProgress(Progress progress) {
        save(progress);
        return progress;
    }

    /**
     * 修改工序
     *
     * @param progress 工序
     * @return 结果
     */
    @Transactional
    @Override
    public Progress updateProgress(Progress progress) {
        updateById(progress);
        return progress;
    }

    /**
     * 批量删除工序
     *
     * @param ids 需要删除的工序主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProgressByIds(Long[] ids) {
        progressMapper.deleteProgressDetailByProgressIds(ids);
        return progressMapper.deleteProgressByIds(ids);
    }

    /**
     * 条件删除
     * @param progress 工序
     * @return
     */
    @Override
    public int delete(Progress progress) {
        QueryWrapper<Progress> qw = new QueryWrapper<>();
        QueryTool.condition(qw, progress);
        return baseMapper.delete(qw);
    }

    /**
     * mp分页
     * @param progress 工序
     * @return
     */
    @Override
    public Pages<Progress> queryPage(Progress progress) {
        QueryWrapper<Progress> qw = new QueryWrapper<>();
        QueryTool.condition(qw, progress);
        IPage<Progress> ipage = new Query<Progress>().getPage(progress);

        IPage<Progress> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    @Override
    public int saveBatchProgress(List<Progress> progressList, SysUser user) {
        Date now = new Date();
        for(Progress progress: progressList) {
            Progress exist = progressMapper.selectAvailableByName(progress.getName());
            if(exist == null) {
                // 新增
                progress.setCreateId(user.getUserId());
                progress.setCreateTime(now);
                progress.setCreateBy(user.getNickName());
                this.insertProgress(progress);
            } else {
                // 修改
                exist.setPrice(progress.getPrice());
                exist.setRemark(progress.getRemark());
                exist.setNo(progress.getNo());
                exist.setUpdateId(user.getUserId());
                exist.setUpdateTime(DateUtils.getNowDate());
                exist.setUpdateBy(user.getNickName());
                this.updateProgress(exist);
            }
        }

        return progressList.size();
    }

    /**
     * 删除工序信息
     *
     * @param id 工序主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProgressById(Long id) {
//        progressMapper.deleteProgressDetailByProgressId(id);
        return progressMapper.deleteProgressById(id);
    }

    /**
     * 新增工序详情信息
     *
     * @param progress 工序对象
     */
    public void insertProgressDetail(Progress progress) {
        List<ProgressDetail> progressDetailList = progress.getProgressDetailList();
        Long id = progress.getId();
        if (StringUtils.isNotNull(progressDetailList))
        {
            List<ProgressDetail> list = new ArrayList<ProgressDetail>();
            for (ProgressDetail progressDetail : progressDetailList)
            {
                progressDetail.setProgressId(id);
                list.add(progressDetail);
            }
            if (list.size() > 0)
            {
                progressMapper.batchProgressDetail(list);
            }
        }
    }
}
